Ext.define('component.user.selectWindow', {
	extend : 'Ext.window.Window',
	callbackFun: function (record) {
		Ext.Msg.alert('提示', '回调函数未配置！');
	},
	config:{  
//      projectId: ''
    },
	initComponent : function() {
		var me = this;
//		var projectId = this.projectId;
		var gridStore = Ext.create('Ext.data.Store', {
		    fields: ["id","userName","userPhone"],
		    pageSize: 10,  //页容量5条数据
		    //是否在服务端排序 （true的话，在客户端就不能排序）
		    remoteSort: false,
		    proxy: {
		        type: 'ajax',
		        url: contextPath+'/customer/customer/userList',
		        reader: {   //这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
		            type: 'json', //返回数据类型为json格式
		            root: 'rows',  //数据
		            totalProperty: 'total' //数据总条数
		        },
		        actionMethods: {read: 'POST'}
		    },
		    listeners: {
	        	"beforeload": function (store, op, options) {  
		      		var params = {
//		                "projectId": projectId
		            };  
		            Ext.apply(store.proxy.extraParams, params);   
	        	}
	    	},
	    	autoLoad: true  //即时加载数据
		});
		var columns = [{
			text : "ID",
			dataIndex : 'id',
			hidden : true
		},{
			text : "用户名称",
			dataIndex : 'userName'
		},{
			text : "用户号码",
			dataIndex : 'userPhone'
		}];
		var bbar=Ext.create('Ext.PagingToolbar', {
			store : gridStore,
			displayInfo : true
		})
		var grid=Ext.create('Ext.grid.Panel', {
			title:"用户列表",
			minHeight:400,
			store : gridStore,
			columns : columns,
			bbar : bbar,
			selModel:  Ext.create('Ext.selection.CheckboxModel', {
				checkOnly:true,
				mode:'single',
				allowDeselect:true
			})
		});

		var form = Ext.create("Ext.form.Panel", {
		    minHeight:60,
		    autoScroll: false,   //自动创建滚动条
		    defaultType: 'textfield',
		    fieldDefaults: {
		        flex: 1,
		        margin: 2,
		        width:80
		    },
		    items: [{
		            xtype: "container",
		            layout: "hbox",
		            anchor: '60%',
		            items: [
		                { xtype: "textfield", id:"userName", fieldLabel: "用户名称",width:80,name:"userName" },
		                { xtype: "button",text: "查询",iconCls:'icon-search', 
			  	        	handler:function(){
			  	        		var userName = Ext.getCmp('userName').getValue();
//			  	        		gridStore.getProxy().extraParams.projectId = projectId;
			  	        		gridStore.getProxy().extraParams.userName = userName;
			  	        		gridStore.load();
			  	        		var pagingTool=Ext.getCmp("pagingTool");
			  	        		pagingTool.moveFirst();//跳至第一页
			  	           }
		                },
		                { xtype: "button",text: "确认选择",iconCls:'icon-add',handler:function(){
		                	var record = grid.getSelectionModel().getLastSelected();
		                	if(null==record){
		                		Ext.Msg.alert('提示', '请选择一个用户！');
		                		return;
		                	}
		                	var jsonRecord=Ext.JSON.encode(record.data);
		                	me.callbackFun(jsonRecord);
		                	me.close();
		                }}
		            ]
		        }  
		    ]
		});

		var mainPanel=Ext.create('Ext.panel.Panel', {
		    items:[form,grid]
		});
		
		var defaultConfig = {
				frame:true,
				title : '选择用户',
				width : 760,
				height : 500,
				layout : 'fit',
				items:[mainPanel],
	            id:'pagingTool'
		};
		Ext.apply(this,defaultConfig);
		this.callParent();
	}
});
